var lei,all,iArray;
var leiArray = [];

var box = document.getElementsByClassName('box')[0];


function rePlay () {
    all = +prompt('请输入您想玩的格数（15以内）');
    if(all > 15) {
        rePlay ();
        return;
    }
    box.style.width = 50 * all + 'px';
    box.style.height = 50 * all + 'px';
    box.style.display = 'flex';
    if(all <= 4) {
        lei = 5
    }else if(all > 4 && all <= 8) {
        lei = 25
    }else if(all > 8 && all <= 12) {
        lei = 35
    }else{
        lei = 50
    }
    box.innerHTML = '';
    for(let i=0;i<Math.pow(all,2);i++) {
        let div = document.createElement('div');
        div.classList.add('item');
        box.appendChild(div);
    }
    leiArray = [];
    for(let i = 0; i<lei;i++) {
        let index = parseInt(Math.random()*Math.pow(all,2),10);
        if(leiArray.indexOf(index) != -1) {
            i--;
        }else{
            leiArray.push(index);
        }
    }
    iArray = [
        [1,all,all+1],
        [-1,1,all-1,all,all+1],
        [-1,all-1,all],
        [-all-1,-all,-1,all-1,all],
        [-all-1,-all,-1],
        [-all-1,-all,-all+1,-1,1],
        [-all,-all+1,1],
        [-all,-all+1,1,all,all+1],
        [-all-1,-all,-all+1,-1,1,all-1,all,all+1]
    ]
    console.log(leiArray)
    game (all,Math.pow(all,2))
}
rePlay();

function game (len) {

    let item = document.getElementsByClassName('item');
    for(let i=0; i<item.length;i++) {
        (function(i){
            item[i].addEventListener('click',function(){
                console.log(this,i);
                if(leiArray.indexOf(i) != -1) {
                    this.style.background = 'red';
                    setTimeout(()=>{
                        alert('游戏结束！');
                        location.reload();
                    },200)
                }else{
                    this.style.background = 'green';
                    let iArray_;
                    let leiNum = 0;
                    if(i < 0 &&  i > len - 1) {
                        iArray_ = iArray[1];
                    }else if(i > len && i % len == len-1) {
                        iArray_ = iArray[3];
                    }else if(i > item.length -1 && i < item.length - len) {
                        iArray_ = iArray[5];
                    }else if(i % len == 0) {
                        iArray_ = iArray[7];
                    }else if(i == 0) {
                        iArray_ = iArray[0];
                    }else if(i == len) {
                        iArray_ = iArray[2];
                    }else if( i == item.length -1 ) {
                        iArray_ = iArray[4];
                    }else if(i == item.length - len) {
                        iArray_ = iArray[6];
                    }else{
                        iArray_ = iArray[8];
                    }
                    for(let j=0;j<iArray_.length;j++) {
                        let leiIndex = i + iArray_[j];
                        console.log(leiIndex,leiArray)
                        if(leiArray.indexOf(leiIndex) != -1) {
                            leiNum++;
                        }
                    }
                    this.innerHTML = leiNum;
                }
            })
        })(i)
    }
}